#include "triangletreemodel.h"
#include <QItemDelegate>
#include <QSpinBox>
#include <QTreeView>
#include <QComboBox>
QDoubleSpinBox *createDSpinBoxForItem(QTreeWidget *tw, QTreeWidgetItem *it, int raw)
{
    QDoubleSpinBox *dsb = new QDoubleSpinBox;
    dsb->setDecimals(20);
    dsb->setMaximum(1000000000);
    dsb->setMinimum(-1000000000);
    tw->setItemWidget(it, raw, dsb);
    return dsb;
}

QComboBox *createEdgeComboboxForItem(QTreeWidget *tw, QTreeWidgetItem *it, int raw)
{
    QComboBox *cmb = new QComboBox;

    cmb->addItem("NONE");
    cmb->setItemData(0, QColor( Qt::blue), Qt::BackgroundRole );
    cmb->addItem("WALL");
    cmb->setItemData(1, QColor( Qt::red), Qt::BackgroundRole );
    cmb->addItem("IN");
    cmb->setItemData(2, QColor( Qt::green), Qt::BackgroundRole );
    cmb->addItem("OUT");
    cmb->setItemData(3, QColor( Qt::yellow), Qt::BackgroundRole );
    tw->setItemWidget(it, raw, cmb);
    return cmb;
}

TriangleTreeModel::TriangleTreeModel(QTreeWidget *tw)
{
    topLevelItem=new QTreeWidgetItem;
    pointsItem = new QTreeWidgetItem(topLevelItem);
    edgesitem = new QTreeWidgetItem(topLevelItem);

    edge12item = new QTreeWidgetItem(edgesitem);
    edge23item = new QTreeWidgetItem(edgesitem);
    edge31item = new QTreeWidgetItem(edgesitem);

    p1item=new QTreeWidgetItem(pointsItem);
    p1xitem=new QTreeWidgetItem(p1item);
    p1yitem=new QTreeWidgetItem(p1item);
    p1paramitem=new QTreeWidgetItem(p1item);

    p2item=new QTreeWidgetItem(pointsItem);
    p2xitem=new QTreeWidgetItem(p2item);
    p2yitem=new QTreeWidgetItem(p2item);
    p2paramitem=new QTreeWidgetItem(p2item);

    p3item=new QTreeWidgetItem(pointsItem);
    p3xitem=new QTreeWidgetItem(p3item);
    p3yitem=new QTreeWidgetItem(p3item);
    p3paramitem = new QTreeWidgetItem(p3item);

    trgparamitem = new QTreeWidgetItem(topLevelItem);

    tw->setColumnCount(2);
    tw->setColumnWidth(0,150);
    tw->addTopLevelItem(topLevelItem);
    topLevelItem->setText(0,"Triangle");
    pointsItem->setText(0,"Points");

    trgparamitem->setText(0,"Param");
    trgparamwidget = createDSpinBoxForItem(tw,trgparamitem,1);

    p1item->setText(0,"1");
    p1xitem->setText(0,"X");
    p1xwidget =  createDSpinBoxForItem(tw,p1xitem,1);

    p1yitem->setText(0,"Y");
    p1ywidget =  createDSpinBoxForItem(tw,p1yitem,1);

    p1paramitem->setText(0,"param");
    p1paramwidget =  createDSpinBoxForItem(tw,p1paramitem,1);

    p2item->setText(0,"2");
    p2xitem->setText(0,"X");
    p2xwidget =  createDSpinBoxForItem(tw,p2xitem,1);

    p2yitem->setText(0,"Y");
    p2ywidget =  createDSpinBoxForItem(tw,p2yitem,1);

    p2paramitem->setText(0,"param");
    p2paramwidget =  createDSpinBoxForItem(tw,p2paramitem,1);

    p3item->setText(0,"3");
    p3xitem->setText(0,"X");
    p3xwidget =  createDSpinBoxForItem(tw,p3xitem,1);

    p3yitem->setText(0,"Y");
    p3ywidget =  createDSpinBoxForItem(tw,p3yitem,1);

    p3paramitem->setText(0,"param");
    p3paramwidget = createDSpinBoxForItem(tw,p3paramitem,1);

    edgesitem->setText(0,"Edges");
    edge12item->setText(0,"1-2");
    edge12widget = createEdgeComboboxForItem(tw,edge12item,1);

    edge23item->setText(0,"2-3");
    edge23widget = createEdgeComboboxForItem(tw,edge23item,1);

    edge31item->setText(0,"3-1");
    edge31widget = createEdgeComboboxForItem(tw,edge31item,1);

    QObject::connect(edge12widget, SIGNAL(currentIndexChanged(int)),this, SLOT(edge12Changed(int)));
    QObject::connect(edge23widget, SIGNAL(currentIndexChanged(int)),this, SLOT(edge23Changed(int)));
    QObject::connect(edge31widget, SIGNAL(currentIndexChanged(int)),this, SLOT(edge31Changed(int)));

    QObject::connect(p1xwidget, SIGNAL(valueChanged(double)),this, SLOT(p1xChanged(double)));
    QObject::connect(p1ywidget, SIGNAL(valueChanged(double)),this, SLOT(p1yChanged(double)));
    QObject::connect(p1paramwidget, SIGNAL(valueChanged(double)),this, SLOT(p1paramChanged(double)));
    QObject::connect(p2xwidget, SIGNAL(valueChanged(double)),this, SLOT(p2xChanged(double)));
    QObject::connect(p2ywidget, SIGNAL(valueChanged(double)),this, SLOT(p2yChanged(double)));
    QObject::connect(p2paramwidget, SIGNAL(valueChanged(double)),this, SLOT(p2paramChanged(double)));
    QObject::connect(p3xwidget, SIGNAL(valueChanged(double)),this, SLOT(p3xChanged(double)));
    QObject::connect(p3ywidget, SIGNAL(valueChanged(double)),this, SLOT(p3yChanged(double)));
    QObject::connect(p3paramwidget, SIGNAL(valueChanged(double)),this, SLOT(p3paramChanged(double)));
    QObject::connect(trgparamwidget, SIGNAL(valueChanged(double)),this, SLOT(trgparamChanged(double)));
}

void TriangleTreeModel::edge12Changed(int a)
{
    trg->getEdge(trg->p1,trg->p2)->type = (Edge::EDGE_TYPE) a;
}

void TriangleTreeModel::edge23Changed(int a)
{
    trg->getEdge(trg->p2,trg->p3)->type = (Edge::EDGE_TYPE) a;
}

void TriangleTreeModel::edge31Changed(int a)
{
    trg->getEdge(trg->p3,trg->p1)->type = (Edge::EDGE_TYPE) a;
}

void TriangleTreeModel::trgparamChanged(double val)
{
    trg->param = val;
}


void TriangleTreeModel::p1xChanged(double val)
{
    trg->p1->x = val;
}

void TriangleTreeModel::p1yChanged(double val)
{
    trg->p1->y = val;
}

void TriangleTreeModel::p1paramChanged(double val)
{
    trg->p1->param = val;
}

void TriangleTreeModel::p2xChanged(double val)
{
    trg->p2->x = val;
}

void TriangleTreeModel::p2yChanged(double val)
{
    trg->p2->y = val;
}

void TriangleTreeModel::p2paramChanged(double val)
{
    trg->p2->param = val;
}

void TriangleTreeModel::p3xChanged(double val)
{
    trg->p3->x = val;
}

void TriangleTreeModel::p3yChanged(double val)
{
    trg->p3->y = val;
}

void TriangleTreeModel::p3paramChanged(double val)
{
    trg->p3->param = val;
}

void TriangleTreeModel::set(Triangle *trg)
{
    this->trg = trg;

    p1xwidget->setValue(trg->p1->x);
    p1ywidget->setValue(trg->p1->y);
    p1paramwidget->setValue(trg->p1->param);


    p2xwidget->setValue(trg->p2->x);
    p2ywidget->setValue(trg->p2->y);
    p2paramwidget->setValue(trg->p2->param);


    p3xwidget->setValue(trg->p3->x);
    p3ywidget->setValue(trg->p3->y);
    p3paramwidget->setValue(trg->p3->param);

    trgparamwidget->setValue(trg->param);

    edge12widget->setCurrentIndex(trg->getEdge(trg->p1,trg->p2)->type);
    edge23widget->setCurrentIndex(trg->getEdge(trg->p2,trg->p3)->type);
    edge31widget->setCurrentIndex(trg->getEdge(trg->p3,trg->p1)->type);
}

